package org.jivesoftware.smackx;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.jivesoftware.smack.Connection;
import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.filter.PacketExtensionFilter;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smackx.packet.MessageEvent;

public class MessageEventManager
{
  private List<MessageEventNotificationListener> messageEventNotificationListeners = new ArrayList();
  private List<MessageEventRequestListener> messageEventRequestListeners = new ArrayList();
  private Connection con;
  private PacketFilter packetFilter = new PacketExtensionFilter("x", "jabber:x:event");
  private PacketListener packetListener;

  public MessageEventManager(Connection paramConnection)
  {
    this.con = paramConnection;
    init();
  }

  public static void addNotificationsRequests(Message paramMessage, boolean paramBoolean1, boolean paramBoolean2, boolean paramBoolean3, boolean paramBoolean4)
  {
    MessageEvent localMessageEvent = new MessageEvent();
    localMessageEvent.setOffline(paramBoolean1);
    localMessageEvent.setDelivered(paramBoolean2);
    localMessageEvent.setDisplayed(paramBoolean3);
    localMessageEvent.setComposing(paramBoolean4);
    paramMessage.addExtension(localMessageEvent);
  }

  public void addMessageEventRequestListener(MessageEventRequestListener paramMessageEventRequestListener)
  {
    synchronized (this.messageEventRequestListeners)
    {
      if (!this.messageEventRequestListeners.contains(paramMessageEventRequestListener))
        this.messageEventRequestListeners.add(paramMessageEventRequestListener);
    }
  }

  public void removeMessageEventRequestListener(MessageEventRequestListener paramMessageEventRequestListener)
  {
    synchronized (this.messageEventRequestListeners)
    {
      this.messageEventRequestListeners.remove(paramMessageEventRequestListener);
    }
  }

  public void addMessageEventNotificationListener(MessageEventNotificationListener paramMessageEventNotificationListener)
  {
    synchronized (this.messageEventNotificationListeners)
    {
      if (!this.messageEventNotificationListeners.contains(paramMessageEventNotificationListener))
        this.messageEventNotificationListeners.add(paramMessageEventNotificationListener);
    }
  }

  public void removeMessageEventNotificationListener(MessageEventNotificationListener paramMessageEventNotificationListener)
  {
    synchronized (this.messageEventNotificationListeners)
    {
      this.messageEventNotificationListeners.remove(paramMessageEventNotificationListener);
    }
  }

  private void fireMessageEventRequestListeners(String paramString1, String paramString2, String paramString3)
  {
    MessageEventRequestListener[] arrayOfMessageEventRequestListener = null;
    synchronized (this.messageEventRequestListeners)
    {
      arrayOfMessageEventRequestListener = new MessageEventRequestListener[this.messageEventRequestListeners.size()];
      this.messageEventRequestListeners.toArray(arrayOfMessageEventRequestListener);
    }
    try
    {
      Method localMethod = MessageEventRequestListener.class.getDeclaredMethod(paramString3, new Class[] { String.class, String.class, MessageEventManager.class });
      for (int i = 0; i < arrayOfMessageEventRequestListener.length; i++)
        localMethod.invoke(arrayOfMessageEventRequestListener[i], new Object[] { paramString1, paramString2, this });
    }
    catch (NoSuchMethodException localNoSuchMethodException)
    {
      localNoSuchMethodException.printStackTrace();
    }
    catch (InvocationTargetException localInvocationTargetException)
    {
      localInvocationTargetException.printStackTrace();
    }
    catch (IllegalAccessException localIllegalAccessException)
    {
      localIllegalAccessException.printStackTrace();
    }
  }

  private void fireMessageEventNotificationListeners(String paramString1, String paramString2, String paramString3)
  {
    MessageEventNotificationListener[] arrayOfMessageEventNotificationListener = null;
    synchronized (this.messageEventNotificationListeners)
    {
      arrayOfMessageEventNotificationListener = new MessageEventNotificationListener[this.messageEventNotificationListeners.size()];
      this.messageEventNotificationListeners.toArray(arrayOfMessageEventNotificationListener);
    }
    try
    {
      Method localMethod = MessageEventNotificationListener.class.getDeclaredMethod(paramString3, new Class[] { String.class, String.class });
      for (int i = 0; i < arrayOfMessageEventNotificationListener.length; i++)
        localMethod.invoke(arrayOfMessageEventNotificationListener[i], new Object[] { paramString1, paramString2 });
    }
    catch (NoSuchMethodException localNoSuchMethodException)
    {
      localNoSuchMethodException.printStackTrace();
    }
    catch (InvocationTargetException localInvocationTargetException)
    {
      localInvocationTargetException.printStackTrace();
    }
    catch (IllegalAccessException localIllegalAccessException)
    {
      localIllegalAccessException.printStackTrace();
    }
  }

  private void init()
  {
    this.packetListener = new PacketListener()
    {
      public void processPacket(Packet paramAnonymousPacket)
      {
        Message localMessage = (Message)paramAnonymousPacket;
        MessageEvent localMessageEvent = (MessageEvent)localMessage.getExtension("x", "jabber:x:event");
        Iterator localIterator;
        if (localMessageEvent.isMessageEventRequest())
        {
          localIterator = localMessageEvent.getEventTypes();
          while (localIterator.hasNext())
            MessageEventManager.this.fireMessageEventRequestListeners(localMessage.getFrom(), localMessage.getPacketID(), ((String)localIterator.next()).concat("NotificationRequested"));
        }
        else
        {
          localIterator = localMessageEvent.getEventTypes();
          while (localIterator.hasNext())
            MessageEventManager.this.fireMessageEventNotificationListeners(localMessage.getFrom(), localMessageEvent.getPacketID(), ((String)localIterator.next()).concat("Notification"));
        }
      }
    };
    this.con.addPacketListener(this.packetListener, this.packetFilter);
  }

  public void sendDeliveredNotification(String paramString1, String paramString2)
  {
    Message localMessage = new Message(paramString1);
    MessageEvent localMessageEvent = new MessageEvent();
    localMessageEvent.setDelivered(true);
    localMessageEvent.setPacketID(paramString2);
    localMessage.addExtension(localMessageEvent);
    this.con.sendPacket(localMessage);
  }

  public void sendDisplayedNotification(String paramString1, String paramString2)
  {
    Message localMessage = new Message(paramString1);
    MessageEvent localMessageEvent = new MessageEvent();
    localMessageEvent.setDisplayed(true);
    localMessageEvent.setPacketID(paramString2);
    localMessage.addExtension(localMessageEvent);
    this.con.sendPacket(localMessage);
  }

  public void sendComposingNotification(String paramString1, String paramString2)
  {
    Message localMessage = new Message(paramString1);
    MessageEvent localMessageEvent = new MessageEvent();
    localMessageEvent.setComposing(true);
    localMessageEvent.setPacketID(paramString2);
    localMessage.addExtension(localMessageEvent);
    this.con.sendPacket(localMessage);
  }

  public void sendCancelledNotification(String paramString1, String paramString2)
  {
    Message localMessage = new Message(paramString1);
    MessageEvent localMessageEvent = new MessageEvent();
    localMessageEvent.setCancelled(true);
    localMessageEvent.setPacketID(paramString2);
    localMessage.addExtension(localMessageEvent);
    this.con.sendPacket(localMessage);
  }

  public void destroy()
  {
    if (this.con != null)
      this.con.removePacketListener(this.packetListener);
  }

  protected void finalize()
    throws Throwable
  {
    destroy();
    super.finalize();
  }
}

/* Location:           D:\hygieia\userclient\common\libs\easemobchat_2.1.8.jar
 * Qualified Name:     org.jivesoftware.smackx.MessageEventManager
 * JD-Core Version:    0.6.1
 */